﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Core.DatabaseAccess;
using PuroGame.Business;

namespace PuroGame.Repositories.Repository
{
    public class UsuarioRepository : Interfaces.IUsuarioRepository
    {
        private Dictionary<string, object> param;
        
        public void Criar(Business.usuario _usuario)
        {
            string query = @"INSERT INTO usuario (email, senha, nome, ip) 
            VALUES (@email, @senha, @nome, @ip)";

            param = new Dictionary<string, object>();
            param.Add("email", _usuario.email);
            param.Add("senha", _usuario.senha);
            param.Add("nome", _usuario.nome);
            param.Add("ip", _usuario.ip);

            Base.Save(query, param);
        }

        public usuario Login(usuario _usuario)
        {
            string query = @"SELECT * FROM usuario WHERE email = @email AND senha = @senha";

            param = new Dictionary<string, object>() 
            { 
                {"email", _usuario.email },
                {"senha", _usuario.senha }
            };

            return Base.Query<Business.usuario>(query, param).FirstOrDefault();
        }

        public bool EmailExiste(string email)
        {
            bool bExiste = false;
            
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("email", email);

            string query = @"SELECT * FROM usuario WHERE email = @email";

            usuario _usuario = Base.Query<usuario>(query, param).FirstOrDefault();

            if (_usuario != null)
                bExiste = true;

            return bExiste;
        }
    }
}
